#include <unistd.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>

#define LOG(level,fmt,args...) printf(fmt,##args)

int main(int argc,char *argv[])
{
    int ret = 0;
    int fd = -1;
    struct sockaddr_in servaddr,cliaddr;
    socklen_t socklen = sizeof(servaddr);

    LOG(0,"just a test!\n");

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr("192.168.8.124");
    servaddr.sin_port = htons(60000);

    fd = socket(AF_INET,SOCK_STREAM,0);

    struct timeval tout;

    tout.tv_sec = 100;
    tout.tv_usec = 0;

    setsockopt(fd,SOL_SOCKET,SO_SNDTIMEO,(void*)&tout,sizeof(tout));
    setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,(void*)&tout,sizeof(tout));

    ret = connect(fd,(struct sockaddr*)&servaddr,socklen); 

    fd_set rdset;
    char recvbuff[2048];
    int recvlen = 0;

    LOG(1,"just a test!\n");

    FD_ZERO(&rdset);
    FD_SET(fd,&rdset);
    while((ret=select(fd+1,&rdset,NULL,NULL,&tout))>0)
    {
	memset(&cliaddr,0,sizeof(cliaddr));
	memset(recvbuff,0,sizeof(recvbuff));
	socklen = sizeof(cliaddr);
	recvlen = recvfrom(fd,recvbuff,sizeof(recvbuff),0,(struct sockaddr*)&cliaddr,&socklen);
	printf("[%s] error = %s\n",strerror(errno));
	printf("ret = %d,recvlen = %d\n",ret,recvlen);
	sleep(1);
    }

    close(fd);
    return 0;
}

